//
//  ViewController.m
//  TouchMeBaby
//
//  Created by 丁诚昊(831) on 15-6-8.
//  Copyright (c) 2015年 丁诚昊(831). All rights reserved.
//

#import "ViewController.h"

#import "OverlaySrollView.h"
#import "TouchsDelayRecognizer.h"
#import "UIView+D_Geometry.h"
#import "DCHBas2DAniGrp.h"

#pragma mark - 关键类
#import "DCH_3DSpaceLear.h"

@interface ViewController() <UIGestureRecognizerDelegate>
/**pusher NSTimer*/
@property (nonatomic, strong) NSTimer* Tpusher;
@end

@implementation ViewController{
    UIView* _canvasView;
    UIScrollView* _scrollView;
    UIVisualEffectView* _VisualBlurView;
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    [DCH_3DSpaceLear arrangeDotsRandomlyInView:self.view];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGRect bounds = self.view.bounds;
    _canvasView = [[UIView alloc] initWithFrame:bounds];
    [self.view addSubview:_canvasView];
    _canvasView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    
    TouchsDelayRecognizer *delayRecognizer = [[TouchsDelayRecognizer alloc] initWithTarget:nil action:nil];
    [_canvasView addGestureRecognizer:delayRecognizer];
    
    short int addedDotsSpaces = arc4random_range(1000, 1500);
    [self addDots:addedDotsSpaces /** 1:0.618 or 1.618:1.0 黄金分割*/ toView:_canvasView];
    
    //1.加入一个滚动视图
    _scrollView = [[OverlaySrollView alloc] initWithFrame:bounds];
    [self.view addSubview:_scrollView];
    
    //2.加入一个blur视图 使用系统自带的visualEffectView
    _VisualBlurView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
    [_scrollView addSubview:_VisualBlurView];
    _VisualBlurView.frame = CGRectMake(0, 0, kscreemWidth, kscreemHeight * .5f);
    
    //3.给滚动视图增加必要的输出条件
    _scrollView.contentSize = CGSizeMake(bounds.size.width, bounds.size.height + _VisualBlurView.frame.size.height);
    _scrollView.contentOffset = CGPointMake(0, _VisualBlurView.frame.size.height);
    [self.view addGestureRecognizer:_scrollView.panGestureRecognizer];
    
    //4.给blur视图添加20个小圆点
    [self addDots:20 toView:_VisualBlurView.contentView];
    [DCH_3DSpaceLear arrangeDotsRandomlyInView:_VisualBlurView.contentView];
    
    [self.view foo];
    
    int slicerNum = arc4random_range(2, 5);
    [DCH_3DSpaceLear arrangeDotsLikeACycle:_canvasView
                                     withR:kscreemWidth/slicerNum /** 东邪／南帝／西毒／北丐*/
                                    PivotX:slicerNum];/** 1金 2木 3水 4火 5土 */
    [[NSRunLoop mainRunLoop] addTimer:self.Tpusher forMode:NSDefaultRunLoopMode];
}

- (NSTimer *)Tpusher{
    if (!_Tpusher) {
        self.Tpusher = [NSTimer timerWithTimeInterval:5.0f target:self selector:@selector(UIrunRender) userInfo:nil repeats:YES];
    }
    
    return _Tpusher;
}

static unsigned long long  counter = 2;
-(void)UIrunRender
{
    counter++;
    
    //产生一个越来越快的衰减时间效果
    if(counter % 3 == 0)
    {
        self.Tpusher.tolerance = 0.33f;
    }
    else
    {
        self.Tpusher.tolerance = 1.0f;
    }
    
    [UIView animateKeyframesWithDuration:self.Tpusher.tolerance*2 delay:.25f options:UIViewKeyframeAnimationOptionCalculationModeCubicPaced animations:^{
        self.view.backgroundColor = [UIView randomColor];
        [DCH_3DSpaceLear arrangeDotsLikeACycle:_canvasView
                                         withR:kscreemWidth/(arc4random_range(3,5)) /** 东邪／南帝／西毒／北丐*/
                                        PivotX:arc4random_range(2, 4)];/** 1金 2木 3水 4火 5土 */
    } completion:^(BOOL finished) {
        self.view.backgroundColor = [UIColor blackColor];
    }];
}


- (void)addDots:(NSUInteger)count toView:(UIView*)view{
    for (NSUInteger i = 0; i < count;  i++) {
        DCH_3DSpaceLear *dots = [DCH_3DSpaceLear fancyColorCypherView];
        [view addSubview:dots];
        
        ///1.0 给每一个dots添加一个长按手势响应方法
        UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(DidlongPressAction:)];
        longPress.cancelsTouchesInView = NO;
        longPress.delegate = self;
        [dots addGestureRecognizer:longPress];
    }
}

- (void) DidlongPressAction:(UILongPressGestureRecognizer*)gesture{
    UIView* dotsView = gesture.view;
    
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:
            //do call callback when user begin 2 grab the element in gesture.view
            [self grabDot:dotsView withGesture:gesture];
            break;
        case UIGestureRecognizerStateChanged:
            [self moveDot:dotsView withGesture:gesture];
            break;
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:
            [self DropDot:dotsView withGesture:gesture];
            break;
        default:
            break;
    }
}

/**移动*/
- (void) grabDot:(UIView*)dot withGesture:(UIGestureRecognizer*)gesture{
    //1.当用户抓起一个图案 先把图案中心点转换到用户手指 在适当方法图案 再进行移动
    dot.center = [self.view convertPoint:dot.center fromView:dot.superview];
    
    [UIView animateWithDuration:.12f animations:^{
        dot.transform = CGAffineTransformMakeScale(1.2f, 1.2f);
        dot.alpha = 0.8f;
        _scrollView.panGestureRecognizer.enabled = NO;
        //在父视图中移动
        [self.view addSubview:dot];
        [self moveDot:dot withGesture:gesture];
    }];
    
    if(CGRectContainsPoint(_VisualBlurView.bounds, dot.center)){
        [DCH_3DSpaceLear arrangeDotsNiftyWithAnimation:_VisualBlurView.contentView];
    }
}

/**移动*/
- (void) moveDot:(UIView*)dot withGesture:(UIGestureRecognizer*)gesture{
    dot.center = [gesture locationInView:self.view];
    _scrollView.panGestureRecognizer.enabled = YES;
}

/**取消*/
- (void) DropDot:(UIView*)dot withGesture:(UIGestureRecognizer*)gesture{
    [UIView animateWithDuration:.2f animations:^{
        dot.transform = CGAffineTransformIdentity;
        dot.alpha = 1.0;
    }];
    
    CGPoint dropPoint = [gesture locationInView:_VisualBlurView];
    if (CGRectContainsPoint(_VisualBlurView.bounds, dropPoint)) {
        [_VisualBlurView.contentView addSubview:dot];
    }else{
        [_canvasView addSubview:dot];
    }
    dot.center = [self.view convertPoint:dot.center toView:dot.superview];
    
    [DCH_3DSpaceLear arrangeDotsNiftyWithAnimation:_VisualBlurView.contentView];
}

#pragma mark -- 手势识别器代理
-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

@end
